package com.letter.otools.day.todo;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.letter.otools.day.R;
import com.letter.otools.day.listener.PositionListener;

import java.util.List;

public class TodoAdapter extends RecyclerView.Adapter<TodoAdapter.ViewHolder> {

    private List<Todo> TodoList;
    private PositionListener positionListener;

    public void setPositionListener(PositionListener positionListener) {
        this.positionListener = positionListener;
    }

    public TodoAdapter(List<Todo> TodoList) {
        this.TodoList = TodoList;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvTitle, tvDate, tvPreview, tvWordCount;
        public ImageView ivWeather, ivMood;

        public ViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvDate = itemView.findViewById(R.id.tvDate);
            tvPreview = itemView.findViewById(R.id.tvPreview);
            tvWordCount = itemView.findViewById(R.id.tvWordCount);
            ivWeather = itemView.findViewById(R.id.ivWeather);
            ivMood = itemView.findViewById(R.id.ivMood);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.todo_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Todo Todo = TodoList.get(position);

        holder.tvTitle.setText(Todo.getTitle());
//        holder.tvDate.setText(DateUtils.formatDate(Todo.getDate()));
        holder.tvPreview.setText(Todo.getContent());
        holder.tvWordCount.setText(Todo.getContent().length() + "字");

        // 设置天气图标
//        int weatherResId = getWeatherIcon(Todo.getWeather());
//        holder.ivWeather.setImageResource(weatherResId);

        // 设置心情图标
//        int moodResId = getMoodIcon(Todo.getMood());
//        holder.ivMood.setImageResource(moodResId);

        // 点击事件
        holder.itemView.setOnClickListener(v -> {
            // 打开日记详情
            positionListener.onPosition(1,Todo);
        });
    }

//    private int getWeatherIcon(String weather) {
//        switch (weather) {
//            case "sunny": return R.drawable.ic_sunny;
//            case "cloudy": return R.drawable.ic_cloudy;
//            case "rainy": return R.drawable.ic_rainy;
//            default: return R.drawable.ic_unknown;
//        }
//    }
//
//    private int getMoodIcon(String mood) {
//        switch (mood) {
//            case "happy": return R.drawable.ic_happy;
//            case "normal": return R.drawable.ic_normal;
//            case "sad": return R.drawable.ic_sad;
//            default: return R.drawable.ic_unknown;
//        }
//    }

    @Override
    public int getItemCount() {
        return TodoList.size();
    }
}
